﻿using System;
using System.Data;
using T.ServiceComponent.Log.SPI;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.SPI;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace T.FAS.Runtime.DataAccess.Common
{
    public class LocalLog : LogBase
    {
        public override void Write(object obj, ExtLogInfo extLogInfo = null)
        {
            if (!ExecuteLogSetting.IsLog || !ExecuteLogSetting.IsLogNormal) return;
            LocalLogService.Write(obj);
        }

        public override void WriteChangeLog(List<DataChangeLog> changeLogs, ExtLogInfo extLogInfo = null)
        {
            if (!ExecuteLogSetting.IsLog || !ExecuteLogSetting.IsLogChange) return;
            LocalLogService.WriteChangeLog(changeLogs);
        }

        public override void WriteError(Exception er, string Msg = null, ExtLogInfo extLogInfo = null)
        {
            if (!ExecuteLogSetting.IsLog || !ExecuteLogSetting.IsLogError) return;
            LocalLogService.WriteError(er, Msg);
        }

        public override void WriteSQL(string SQL, IDbDataParameter[] paras = null, SQLLogType type = SQLLogType.DQL, ExtLogInfo extLogInfo = null)
        {
            if (!ExecuteLogSetting.IsLog || !ExecuteLogSetting.IsLogSQL) return;
            LocalLogService.WriteSQL(SQL, paras, type, extLogInfo);
        }

        public override void WriteStateChangeLog(List<StateMachineChangedMsg> msgs, ExtLogInfo extLogInfo = null)
        {
            if (!ExecuteLogSetting.IsLog || !ExecuteLogSetting.IsLogState) return;
            LocalLogService.WriteStateChangeLog(msgs);
        }

        public override void CDOTraceLog(CompositeDataObject cdo, TRecord record, InjectionMethodType opType, string msg = null, object extObj = null)
        {
            if (!ExecuteLogSetting.IsLog || !ExecuteLogSetting.IsLogTrace) return;
            try
            {
                if (cdo == null || record == null) return;
                var pkVal = GetCDOPkValue(cdo, record)?.ToString();
                var currTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                var traceInfo = new
                {
                    CurrentTime = currTime,
                    Msg = msg,
                    PkVal = pkVal,
                    OpType = opType.ToString(),
                };
                TrackLog traceLog = new TrackLog
                {
                    FuncID = cdo.Code,
                    DataKey = pkVal,
                    UserID = UserUtil.GetCurrentUserID(),
                    TraceInfo = JsonConvert.SerializeObject(traceInfo, Setting),
                };
                traceLog.Ext10 = currTime;
                if (extObj != null)
                    traceLog.Ext12 = JsonConvert.SerializeObject(extObj, Setting);
                LocalLogService.WriteTrace(traceLog);
            }
            catch (Exception e)
            {
                LocalLogService.Write(e);
            }
        }
    }
}